package
{
	import com.tools.Timing;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	public class Player extends Sprite{
		private var route:Array;
		private var index:int=0;
		private var startPoint:Point;
		private var toPoint:Point;
		private var speed:int=5;
		public function Player()
		{
			super();
		}
		/**rt路径，px水平偏移，py垂直偏移**/
		public function move(rt:Array,px:int=0,py:int=0):void{
			if(rt.length<1)return;
			route=rt;
			index=0;
			this.x=int(route[index][0])+px;//+Math.random()*50;
			this.y=int(route[index][1])+py;//+Math.random()*50;
			
			index++;
			toPoint=new Point(route[index][0],int(route[index][1]));
			startPoint=new Point(this.x,this.y)
			Timing.self.addEnterFrame("player_auto_move",autoMove);
		}
		public function autoMove():void{
			var dx:int=(toPoint.x-this.x)/Math.abs(toPoint.x-this.x);//方向
			
			var dy:int=(toPoint.y-this.y)/Math.abs(toPoint.y-this.y);
			this.scaleX=dx;
			if(Math.abs(toPoint.x-this.x)>speed)this.x+=speed*dx;
			if(Math.abs(toPoint.y-this.y)>speed)this.y+=speed*dy;
			if(Math.abs(toPoint.x-this.x)<(speed)*2&&Math.abs(toPoint.y-this.y)<(speed)*2){
				index++;
				if(index<route.length){
					toPoint=new Point(route[index][0],int(route[index][1]));
					startPoint=new Point(this.x,this.y);
				}else{
					Timing.self.removeEnterFrame("player_auto_move");
				}
			}
		}
	}
}